package pl.prv.polanie.server.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.hibernate.exception.ConstraintViolationException;

import pl.prv.polanie.client.dto.AcademicTitleDTO;
import pl.prv.polanie.client.dto.AdminUserDTO;
import pl.prv.polanie.client.dto.BuildingDTO;
import pl.prv.polanie.client.dto.CSAcademicTitleDTO;
import pl.prv.polanie.client.dto.CSBuildingDTO;
import pl.prv.polanie.client.dto.CSCalendarDayDTO;
import pl.prv.polanie.client.dto.CSDeclarationDTO;
import pl.prv.polanie.client.dto.CSGroupDTO;
import pl.prv.polanie.client.dto.CSLecturerDTO;
import pl.prv.polanie.client.dto.CSPreferenceDTO;
import pl.prv.polanie.client.dto.CSPreferenceTypeDTO;
import pl.prv.polanie.client.dto.CSRoomDTO;
import pl.prv.polanie.client.dto.CSRoomTypeDTO;
import pl.prv.polanie.client.dto.CSSemesterDTO;
import pl.prv.polanie.client.dto.CSSemesterTypeDTO;
import pl.prv.polanie.client.dto.CSSemestersYearGroupsDTO;
import pl.prv.polanie.client.dto.CSStudiesTypeDTO;
import pl.prv.polanie.client.dto.CSSubgroupDTO;
import pl.prv.polanie.client.dto.CSSubgroupTypeDTO;
import pl.prv.polanie.client.dto.CSSubjectDTO;
import pl.prv.polanie.client.dto.CSSubjectEventDTO;
import pl.prv.polanie.client.dto.CSSubjectNameDTO;
import pl.prv.polanie.client.dto.CSSubjectTypeDTO;
import pl.prv.polanie.client.dto.CSYearGroupNameDTO;
import pl.prv.polanie.client.dto.CalendarDayDTO;
import pl.prv.polanie.client.dto.DeclarationDTO;
import pl.prv.polanie.client.dto.GroupDTO;
import pl.prv.polanie.client.dto.LecturerDTO;
import pl.prv.polanie.client.dto.PreferenceDTO;
import pl.prv.polanie.client.dto.PreferenceTypeDTO;
import pl.prv.polanie.client.dto.RoomDTO;
import pl.prv.polanie.client.dto.RoomTypeDTO;
import pl.prv.polanie.client.dto.SemesterDTO;
import pl.prv.polanie.client.dto.SemesterTypeDTO;
import pl.prv.polanie.client.dto.SemestersYearGroupsDTO;
import pl.prv.polanie.client.dto.StudiesTypeDTO;
import pl.prv.polanie.client.dto.SubgroupDTO;
import pl.prv.polanie.client.dto.SubgroupTypeDTO;
import pl.prv.polanie.client.dto.SubjectDTO;
import pl.prv.polanie.client.dto.SubjectEventDTO;
import pl.prv.polanie.client.dto.SubjectNameDTO;
import pl.prv.polanie.client.dto.SubjectTypeDTO;
import pl.prv.polanie.client.dto.TestDTO;
import pl.prv.polanie.client.dto.YearGroupNameDTO;
import pl.prv.polanie.client.dto.transport.CSTDuplicatingSemesterReportDTO;
import pl.prv.polanie.client.service.PlannerService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;


public class PlannerServiceImpl extends RemoteServiceServlet implements PlannerService {
	
	private static final long serialVersionUID = 1L;
	
	
	private static Logger logger = Logger.getLogger(PlannerServiceImpl.class);
	
//	static {
////		BasicConfigurator.configure();
//		try {
////			logger.addAppender(new FileAppender(new PatternLayout("%-4r [%t] %-5p %c %x - %m%n"), "logi.txt"));
//			
//		} catch (Exception e) {
//			
//		}
//		logger.debug("asd");
//	}
	
	
	
	
	
	//zwraca dni podpiete do semestru
	public CSCalendarDayDTO[] getCSCalendarDaysBySemesterID(Long semesterID) {
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;

		
		s.beginTransaction();
		
		String q = 	"from CalendarDayDTO cd " +
						"where cd.semester.id = :semesterID";
		
		Query query = s.createQuery(q).setLong("semesterID", semesterID);
		
		List<CalendarDayDTO> list = query.list();

		s.getTransaction().commit(); s.close();
		
		
		CalendarDayDTO[] calendarDays = list.toArray(new CalendarDayDTO[0]);		
		logger.debug("downloaded " + calendarDays.length + " calendar days for semester.id " + semesterID);
		CSCalendarDayDTO[] csCalendarDays = PlannerServiceUtil.convertFromCalendarDayDTOToCSCalendarDayDTO(calendarDays);

		return csCalendarDays;
	}

	
	public CSSubjectEventDTO[] getSubjectEventsByGroupID(Long groupID) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		String q = 	"from SubjectEventDTO se " +
						"where se.subgroup.group.id = :groupID";
		
		Query query = s.createQuery(q).setLong("groupID", groupID);
		
		List list = query.list();
		SubjectEventDTO[] subjectEvents = new SubjectEventDTO[list.size()];
		
		int i=0;
		
		for (Iterator it = list.iterator(); it.hasNext();) {
			SubjectEventDTO se = (SubjectEventDTO) it.next();
			subjectEvents[i] = se;
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		
		
		if (subjectEvents.length > 0) {
			SubjectEventDTO l = subjectEvents[0];
			//System.out.println("SUBJECT_EVENT => " + l.toLabel());
		}
		//s.evict(list);
		
		//System.out.println("pobrano SubjectEvents dla groupID = " + groupID + " w liczbie: " + list.size());
		
		
		for (int j = 0; j < subjectEvents.length; j++) {
			//System.out.println(j + ". " + subjectEvents[j]);
		}
		
		CSSubjectEventDTO[] ses = PlannerServiceUtil.convertFromSubjectEventDTOToCSSubjectEventDTO(subjectEvents);

		return ses;
	}
	
	public CSSubjectEventDTO[] getCSSubjectEventsByGroupID(Long groupID) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		String q = 	"from SubjectEventDTO se " +
						"where se.subgroup.group.id = :groupID";
		
		Query query = s.createQuery(q).setLong("groupID", groupID);
		
		List list = query.list();
		SubjectEventDTO[] subjectEvents = new SubjectEventDTO[list.size()];
		
		int i=0;
		
		for (Iterator it = list.iterator(); it.hasNext();) {
			SubjectEventDTO se = (SubjectEventDTO) it.next();
			subjectEvents[i] = se;
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		
		
		if (subjectEvents.length > 0) {
			SubjectEventDTO l = subjectEvents[0];
			//System.out.println("SUBJECT_EVENT => " + l.toLabel());
		}
		logger.info("downloaded " + subjectEvents.length + " subjectEvents for group.id: " + groupID);

		
		CSSubjectEventDTO[] csSubjectEvents = PlannerServiceUtil.convertFromSubjectEventDTOToCSSubjectEventDTO(subjectEvents);
		
		return csSubjectEvents;
	}

	
	/**
	 * Returns array of SEs sorted by calendarDay and startTime.
	 * @param semesterID
	 * @return
	 */
	public CSSubjectEventDTO[] getCSSubjectEventsBySemesterID(Long semesterID) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		SubjectEventDTO[] subjectEvents = null;
		
		try {
			s.beginTransaction();
			
			String q = 	"from SubjectEventDTO se " +
			"where se.subgroup.group.semestersYearGroup.semester.id = :semesterID " +
			"order by se.calendarDay, se.startTime";
			
			Query query = s.createQuery(q).setLong("semesterID", semesterID);
			
			List<SubjectEventDTO> list = query.list();
			subjectEvents = list.toArray(new SubjectEventDTO[0]);
			
			s.getTransaction().commit(); s.close();
			
			
			logger.info("downloaded " + subjectEvents.length + " subjectEvents for semester.id: " + semesterID);

			for (int i = 0; i < subjectEvents.length; i++) {
				logger.debug(i + ". " + subjectEvents[i].toString());
			}
			
		} catch (Exception e) {
			logger.warn("error while trying to download SubjectEvents for semester.id: " + semesterID);
		}
		
		CSSubjectEventDTO[] csSubjectEvents = null;
		if (subjectEvents.length > 0) {
			csSubjectEvents = PlannerServiceUtil.convertFromSubjectEventDTOToCSSubjectEventDTO(subjectEvents);
		}
		
		return csSubjectEvents;
	}

	public CSDeclarationDTO[] getCSDeclarationsBySemesterID(Long semesterID) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		String q = 	"from DeclarationDTO d " +
						"where d.group.semestersYearGroup.semester.id = :semesterID";
		
		Query query = s.createQuery(q).setLong("semesterID", semesterID);
		
		List list = query.list();
		DeclarationDTO[] declarations = new DeclarationDTO[list.size()];
		
		int i=0;
		
		for (Iterator it = list.iterator(); it.hasNext();) {
			DeclarationDTO d = (DeclarationDTO) it.next();
			declarations[i] = d;
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		
		
		if (declarations.length > 0) {
			DeclarationDTO d = declarations[0];
			//System.out.println("DECLARATION => " + d.toLabel());
		}
		//s.evict(list);
		
		//System.out.println("pobrano Declarations dla groupID = " + semesterID + " w liczbie: " + list.size());
		
		
		for (int j = 0; j < declarations.length; j++) {
			//System.out.println(j + ". " + declarations[j]);
		}

		CSDeclarationDTO[] csDeclarations = PlannerServiceUtil.convertFromDeclarationDTOToCSDeclarationDTO(declarations);
		//System.out.println("PlannerServiceImpl.getCSDeclarationsBySemesterID(): mamy tablice " + csDeclarations.length);
		
		return csDeclarations;
	}

	
	public CSSubjectEventDTO[] getCSSubjectEventsByRoomID(Long roomID, Long semesterID) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		String q = 	"from SubjectEventDTO se " +
						"where se.room.id = :roomID " +
						"and se.calendarDay.semester.id = :semesterID";
		
		Query query = s.createQuery(q).setLong("roomID", roomID).setLong("semesterID", semesterID);
		
		List list = query.list();
		SubjectEventDTO[] subjectEvents = new SubjectEventDTO[list.size()];
		
		int i=0;
		
		for (Iterator it = list.iterator(); it.hasNext();) {
			SubjectEventDTO se = (SubjectEventDTO) it.next();
			subjectEvents[i] = se;
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		
		
		if (subjectEvents.length > 0) {
			SubjectEventDTO l = subjectEvents[0];
			////System.out.println("SUBJECT_EVENT => " + l.toLabel());
		}
		//s.evict(list);
		
		//System.out.println("pobrano SubjectEvents dla roomID = " + roomID + ", semesterID: " + " w liczbie: " + list.size());
		
		
		for (int j = 0; j < subjectEvents.length; j++) {
			//System.out.println(j + ". " + subjectEvents[j]);
		}

		CSSubjectEventDTO[] csSubjectEvents = PlannerServiceUtil.convertFromSubjectEventDTOToCSSubjectEventDTO(subjectEvents);
		//System.out.println("PlannerServiceImpl.getCSSubjectEventsByRoomID(): mamy tablice " + csSubjectEvents.length);
		
		return csSubjectEvents;
	}
	
	public CSSubjectEventDTO[] getCSSubjectEventsByLecturerID(Long lecturerID, Long semesterID) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		String q = 	"from SubjectEventDTO se " +
						"where se.lecturer.id = :lecturerID " +
						"and se.calendarDay.semester.id = :semesterID";
		
		Query query = s.createQuery(q).setLong("lecturerID", lecturerID).setLong("semesterID", semesterID);
		
		List list = query.list();
		SubjectEventDTO[] subjectEvents = new SubjectEventDTO[list.size()];
		
		int i=0;
		
		for (Iterator it = list.iterator(); it.hasNext();) {
			SubjectEventDTO se = (SubjectEventDTO) it.next();
			subjectEvents[i] = se;
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		
		
		if (subjectEvents.length > 0) {
			SubjectEventDTO l = subjectEvents[0];
			//System.out.println("SUBJECT_EVENT => " + l.toLabel());
		}
		//s.evict(list);
		
		//System.out.println("pobrano SubjectEvents dla roomID = " + lecturerID + ", semesterID: " + " w liczbie: " + list.size());
		
		
		for (int j = 0; j < subjectEvents.length; j++) {
			//System.out.println(j + ". " + subjectEvents[j]);
		}

		CSSubjectEventDTO[] csSubjectEvents = PlannerServiceUtil.convertFromSubjectEventDTOToCSSubjectEventDTO(subjectEvents);
		//System.out.println("PlannerServiceImpl.getCSSubjectEventsByLecturerID(): mamy tablice " + csSubjectEvents.length);
		
		return csSubjectEvents;
	}
	

//	public PreferenceDTO[] getLecturerPreferencesBySemesterGroupID(Long lecturerID, Long semesterID) {
//		
//		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
//		
//		s.beginTransaction();
//		
//		String q = 	"from PreferenceDTO p " +
//						"where p.lecturer.id = :lecturerID" +
//						" and p.calendarDay.semester.id = :semesterID";
//		
//		Query query = s.createQuery(q).setLong("lecturerID", lecturerID).setLong("semesterID", semesterID);
//		
//		
//		List list = query.list();
//		PreferenceDTO[] lecturerPreferences = new PreferenceDTO[list.size()];
//		
//		int i=0;
//		
//		for (Iterator it = list.iterator(); it.hasNext();) {
//			PreferenceDTO lp = (PreferenceDTO) it.next();
//			lecturerPreferences[i] = lp;
//			i++;
//		}
//		
//		s.getTransaction().commit(); s.close();
//		
//		
//		if (lecturerPreferences.length > 0) {
//			PreferenceDTO l = lecturerPreferences[0];
//			//System.out.println("LECTURER_PREFERENCES => " + l.toLabel());
//		}
//		//s.evict(list);
//		
//		//System.out.println("pobrano Preferences dla lecturerID = " + lecturerID +
////				", semesterID = " + semesterID +" w liczbie: " + list.size());
//		
//		
//		for (int j = 0; j < lecturerPreferences.length; j++) {
//			//System.out.println(j + ". " + lecturerPreferences[j]);
//		}
//
//		return lecturerPreferences;
//	}
	
	
	
//	public CSPreferenceDTO[] getCSLecturerPreferencesBySemesterGroupID(Long lecturerID, Long semesterID) {
//		
//		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
//		
//		s.beginTransaction();
//		
//		String q = 	"from PreferenceDTO p " +
//						"where p.lecturer.id = :lecturerID" +
//						" and p.calendarDay.semester.id = :semesterID";
//		
//		Query query = s.createQuery(q).setLong("lecturerID", lecturerID).setLong("semesterID", semesterID);
//		
//		
//		List list = query.list();
//		PreferenceDTO[] lecturerPreferences = new PreferenceDTO[list.size()];
//		
//		int i=0;
//		
//		for (Iterator it = list.iterator(); it.hasNext();) {
//			PreferenceDTO lp = (PreferenceDTO) it.next();
//			lecturerPreferences[i] = lp;
//			i++;
//		}
//		
//		s.getTransaction().commit(); s.close();
//		
//		if (lecturerPreferences.length > 0) {
//			PreferenceDTO l = lecturerPreferences[0];
//			//System.out.println("LECTURER_PREFERENCES => " + l.toLabel());
//		}
//		//s.evict(list);
//		
//		//System.out.println("pobrano Preferences dla lecturerID = " + lecturerID +
////				", semesterID = " + semesterID +" w liczbie: " + list.size());
//		
//		
//		for (int j = 0; j < lecturerPreferences.length; j++) {
//			//System.out.println(j + ". " + lecturerPreferences[j]);
//		}
//		CSPreferenceDTO[] csPreferences = PlannerServiceUtil.convertFromPreferenceDTOToCSPreferenceDTO(lecturerPreferences);
//		return csPreferences;
//	}
	
	
	
	/**
	 * Zapisuje badz aktualizuje obiekt LecturerPreferencesDTO w bazie danych.
	 */
	public long savePreference(CSPreferenceDTO csPreferenceDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.savePreference(), preference: " + csPreferenceDTO);
		
		PreferenceDTO dto = null;
		
		if (csPreferenceDTO.getId() <= 0) {
			dto = new PreferenceDTO();
		} else {
			dto = (PreferenceDTO)s.load(PreferenceDTO.class, new Long(csPreferenceDTO.getId()));
		}
		
		CalendarDayDTO cd = (CalendarDayDTO)s.load(CalendarDayDTO.class, new Long(csPreferenceDTO.getCalendarDayID()));
		dto.setCalendarDay(cd);
		
		LecturerDTO l = (LecturerDTO) s.load(LecturerDTO.class, new Long(csPreferenceDTO.getLecturerID()));
		dto.setLecturer(l);
		
		PreferenceTypeDTO pt = (PreferenceTypeDTO) s.load(PreferenceTypeDTO.class, new Long(csPreferenceDTO.getPreferenceTypeID()));
		dto.setPreferenceType(pt);
		
		dto.setStartTime(csPreferenceDTO.getStartTime());
		dto.setDuration(csPreferenceDTO.getDuration());
		
		
		
		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("PROORROR " + dto.toLabel());
			//System.out.println("blad > " + e.toString());
		}
		
		return dto.getId();
	}
	
	public void removePreference(CSPreferenceDTO csPreferenceDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.deletePreference(), preference: " + csPreferenceDTO);
		
		
		
		PreferenceDTO dto = null;
		
		dto = (PreferenceDTO)s.load(PreferenceDTO.class, new Long(csPreferenceDTO.getId()));
		
		try {
			s.beginTransaction();
			s.delete(dto);
			s.getTransaction().commit(); s.close();
			logger.info("Preference deleted, id: " + csPreferenceDTO.getId());
		} catch ( HibernateException e ) {
			logger.warn("Preference not deleted, id: " + csPreferenceDTO.getId());
			//System.out.println("blad > " + e.toString());
		}
	}
	
	public CSSubjectEventDTO[] getSubjectEvents() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		List list = s.createQuery("from SubjectEventDTO").list();
		SubjectEventDTO[] subjectEvents = new SubjectEventDTO[list.size()];
		
		int i=0;
		
		for (Iterator it = list.iterator(); it.hasNext();) {
			SubjectEventDTO se = (SubjectEventDTO) it.next();
			subjectEvents[i] = se;
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		SubjectEventDTO l = subjectEvents[0];
		//s.evict(list);
		
		for (int j = 0; j < subjectEvents.length; j++) {
			//System.out.println(j + ". " + subjectEvents[j]);
		}
		//System.out.println("SUBJECT_EVENT => " + l.toLabel());
		
		CSSubjectEventDTO[] ses = PlannerServiceUtil.convertFromSubjectEventDTOToCSSubjectEventDTO(subjectEvents);
		return ses;
	}
	
	
	
	
	public CSSubgroupDTO[] getSubgroups() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		List list = s.createQuery("from SubgroupDTO").list();
		SubgroupDTO[] subgroups = new SubgroupDTO[list.size()];
		
		int i=0;
		
		for (Iterator it = list.iterator(); it.hasNext();) {
			SubgroupDTO sg = (SubgroupDTO) it.next();
			subgroups[i] = sg;
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		SubgroupDTO l = subgroups[0];
		//s.evict(list);
		
		for (int j = 0; j < subgroups.length; j++) {
			//System.out.println(j + ". " + subgroups[j]);
		}
		//System.out.println("SUBGROUP => " + l.toLabel());
		CSSubgroupDTO[] csSubgroups = PlannerServiceUtil.convertFromSubgroupDTOToCSSubgroupDTO(subgroups);

		return csSubgroups;
	}
	
	
	public CSSubgroupDTO[] getSubgroupsBySemesterID(Long semesterID) {
		
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		SubgroupDTO[] subgroups = null;
		
		
		
		try {
			
			s.beginTransaction();
			
			String q = 	"from SubgroupDTO sg " +
							"where sg.group.semestersYearGroup.semester.id = :semesterID";
			
			Query query = s.createQuery(q).setLong("semesterID", semesterID);
			
			List list = query.list();
			subgroups = new SubgroupDTO[list.size()];
			
			int i=0;
			
			for (Iterator it = list.iterator(); it.hasNext();) {
				SubgroupDTO sg = (SubgroupDTO) it.next();
				subgroups[i] = sg;
				i++;
			}
			
			s.getTransaction().commit(); s.close();
			
			
			if (subgroups.length > 0) {
				SubgroupDTO l = subgroups[0];
				////System.out.println("SUBJECT_EVENT => " + l.toLabel());
			}
			//s.evict(list);
			
			//System.out.println("pobrano Subgroups dla semesterID = " + semesterID + ": " + " w liczbie: " + list.size());
			
			
			for (int j = 0; j < subgroups.length; j++) {
				//System.out.println(j + ". " + subgroups[j]);
			}
			
		} catch (Exception e) {
			logger.warn("error while trying to download subgroups for semester.id: " + semesterID);
		}
		
		
		
		CSSubgroupDTO[] csSubgroups = null;
		if (subgroups != null && subgroups.length > 0) {
			csSubgroups = PlannerServiceUtil.convertFromSubgroupDTOToCSSubgroupDTO(subgroups);
		}
		return csSubgroups;

	}

	
	
	public CSGroupDTO[] getGroups() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		List list = s.createQuery("from GroupDTO").list();
		GroupDTO[] groups = new GroupDTO[list.size()];
		
		int i=0;
		
		for (Iterator it = list.iterator(); it.hasNext();) {
			GroupDTO l = (GroupDTO) it.next();
			groups[i] = l;
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		GroupDTO l = groups[0];
		//s.evict(list);
		
		for (int j = 0; j < groups.length; j++) {
			//System.out.println(j + ". " + groups[j]);
		}
		//System.out.println("GROUP => " + l.toLabel());
		CSGroupDTO[] csGroups = PlannerServiceUtil.convertFromGroupDTOToCSGroupDTO(groups);
		
		return csGroups;
	}
	
	
	
	public CSGroupDTO[] getGroupsBySemesterID(Long semesterID) {
		
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		String q = 	"from GroupDTO g " +
						"where g.semestersYearGroup.semester.id = :semesterID";
		
		Query query = s.createQuery(q).setLong("semesterID", semesterID);
		
		List list = query.list();
		GroupDTO[] groups = new GroupDTO[list.size()];
		
		int i=0;
		
		for (Iterator it = list.iterator(); it.hasNext();) {
			GroupDTO g = (GroupDTO) it.next();
			groups[i] = g;
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		
		
		if (groups.length > 0) {
			GroupDTO l = groups[0];
			////System.out.println("SUBJECT_EVENT => " + l.toLabel());
		}
		//s.evict(list);
		
		//System.out.println("\n\n\n\n\n");
		
		//System.out.println("pobrano Groups dla semesterID = " + semesterID + ": " + " w liczbie: " + list.size());
		
		
		for (int j = 0; j < groups.length; j++) {
			//System.out.println(j + ". " + groups[j]);
		}

		CSGroupDTO[] csGroups = PlannerServiceUtil.convertFromGroupDTOToCSGroupDTO(groups);
		//System.out.println("PlannerServiceImpl.getCSGroupsBySemesterID(): mamy tablice " + csGroups.length);
		
		return csGroups;
	}

	
	
	
	
	public CSSemestersYearGroupsDTO[] getCSSemestersYearGroupsBySemesterID(Long semesterID) {
		
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		String q = 	"from SemestersYearGroupsDTO syg " +
						"where syg.semester.id = :semesterID";
		
		Query query = s.createQuery(q).setLong("semesterID", semesterID);
		
		List list = query.list();
		SemestersYearGroupsDTO[] sygs = new SemestersYearGroupsDTO[list.size()];
		
		int i=0;
		
		for (Iterator it = list.iterator(); it.hasNext();) {
			SemestersYearGroupsDTO syg = (SemestersYearGroupsDTO) it.next();
			sygs[i] = syg;
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		
		
//		if (sygs.length > 0) {
//			SemestersYearGroupsDTO syg = sygs[0];
//			////System.out.println("SUBJECT_EVENT => " + l.toLabel());
//		}
		//s.evict(list);
		
		//System.out.println("pobrano SemestersYearGroups dla semesterID = " + semesterID + ": " + " w liczbie: " + list.size());
		
		
		for (int j = 0; j < sygs.length; j++) {
			//System.out.println(j + ". " + sygs[j]);
		}

		CSSemestersYearGroupsDTO[] csSemestersYearGroups = PlannerServiceUtil.convertFromSemestersYearGroupsDTOToCSSemestersYearGroupsDTO(sygs);
		//System.out.println("PlannerServiceImpl.getCSSemestersYearGroupsBySemesterID(): mamy tablice " + csSemestersYearGroups.length);
		
		return csSemestersYearGroups;
	}

	
	
	
	
	

	
	
	
	public CSSemestersYearGroupsDTO[] getSemestersYearGroups() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		List list = s.createQuery("from SemestersYearGroupsDTO").list();
		SemestersYearGroupsDTO[] semestersYearGroups = new SemestersYearGroupsDTO[list.size()];
		
		int i=0;
		
		for (Iterator it = list.iterator(); it.hasNext();) {
			SemestersYearGroupsDTO l = (SemestersYearGroupsDTO) it.next();
			semestersYearGroups[i] = l;
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		SemestersYearGroupsDTO l = semestersYearGroups[0];
		//s.evict(list);
		
		for (int j = 0; j < semestersYearGroups.length; j++) {
			//System.out.println(j + ". " + semestersYearGroups[j]);
		}
		//System.out.println("SEMESTER_YEAR_GROUP => " + l.toLabel());
		
		CSSemestersYearGroupsDTO[] csSYG = PlannerServiceUtil.convertFromSemestersYearGroupsDTOToCSSemestersYearGroupsDTO(semestersYearGroups);
		return csSYG;
	}
	
	public CSYearGroupNameDTO[] getYearGroupNames() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		List list = s.createQuery("from YearGroupNameDTO").list();
		YearGroupNameDTO[] yearGroupNames = new YearGroupNameDTO[list.size()];
		
		int i=0;
		
		for (Iterator it = list.iterator(); it.hasNext();) {
			YearGroupNameDTO l = (YearGroupNameDTO) it.next();
			yearGroupNames[i] = l;
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		YearGroupNameDTO l = yearGroupNames[0];
		//s.evict(list);
		
		for (int j = 0; j < yearGroupNames.length; j++) {
			//System.out.println(j + ". " + yearGroupNames[j]);
		}
		//System.out.println("YEAR_GROUP_NAME => " + l.getName());
		
		CSYearGroupNameDTO[] csYGNs = PlannerServiceUtil.convertFromYearGroupNameDTOToCSYearGroupNameDTO(yearGroupNames);
		return csYGNs;
	}
	
	
	
	public CSSemesterDTO[] getCSSemesters() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		SemesterDTO[] semesters = null;
		
		try {
			s.beginTransaction();
			
			List list = s.createQuery("from SemesterDTO").list();
			semesters = new SemesterDTO[list.size()];
			
			int i=0;
			
			for (Iterator it = list.iterator(); it.hasNext();) {
				SemesterDTO l = (SemesterDTO) it.next();
				semesters[i] = l;
				i++;
			}
			
			s.getTransaction().commit(); s.close();
			logger.info("Loaded semesters " + list.size());
		} catch (Exception e) {
			logger.warn("Not loaded semesters, " + e.getMessage());
		}
		
		
		CSSemesterDTO[] csSemesters = null;
		if (semesters != null) {
			csSemesters= PlannerServiceUtil.convertFromSemesterDTOToCSSemesterDTO(semesters);
			
			for (int i = 0; i < csSemesters.length; i++) {
				logger.info(i+ ", " + csSemesters[i]);
			}
		}
		
		
		
		return csSemesters;
	}
	
	
//	public CSYearNumberDTO[] getYearNumbers() {
//		
//		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
//		YearNumberDTO[] yns = new YearNumberDTO[0];
//		try {
//			s.beginTransaction();
//			List list = s.createQuery("from YearNumberDTO").list();
//			s.getTransaction().commit(); s.close();
//
//			yns = new YearNumberDTO[list.size()];
//			int i=0;
//			
//			for (Iterator it = list.iterator(); it.hasNext();) {
//				YearNumberDTO yn = (YearNumberDTO) it.next();
//				yns[i] = yn;
//				i++;
//			}
//			
//		} catch ( ConstraintViolationException e ) {
//			logger.warn("ConstraintViolationException. " + e.getMessage());
//		} catch ( Exception e ) {
//			//System.out.println("EXCEPTION: Exception: " + e.getMessage());
//		}
//		
//		CSYearNumberDTO[] csYNs = PlannerServiceUtil.convertFromYearNumberDTOToCSYearNumberDTO(yns);
//		
//		return csYNs;
//	}

	
	
	public CSSemesterTypeDTO[] getSemesterTypes() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		SemesterTypeDTO[] sts = new SemesterTypeDTO[0];
		try {
			s.beginTransaction();
			List list = s.createQuery("from SemesterTypeDTO").list();
			s.getTransaction().commit(); s.close();

			sts = new SemesterTypeDTO[list.size()];
			int i=0;
			
			for (Iterator it = list.iterator(); it.hasNext();) {
				SemesterTypeDTO st = (SemesterTypeDTO) it.next();
				sts[i] = st;
				i++;
			}
			
		} catch ( ConstraintViolationException e ) {
			logger.warn("ConstraintViolationException. " + e.getMessage());
		} catch ( Exception e ) {
			//System.out.println("EXCEPTION: Exception: " + e.getMessage());
		}
		
		CSSemesterTypeDTO[] csSTs = PlannerServiceUtil.convertFromSemesterTypeDTOToCSSemesterTypeDTO(sts);
		
		return csSTs;
	}


	
	public CSStudiesTypeDTO[] getStudiesTypes() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		StudiesTypeDTO[] sts = new StudiesTypeDTO[0];
		try {
			s.beginTransaction();
			List list = s.createQuery("from StudiesTypeDTO").list();
			s.getTransaction().commit(); s.close();

			sts = new StudiesTypeDTO[list.size()];
			int i=0;
			
			for (Iterator it = list.iterator(); it.hasNext();) {
				StudiesTypeDTO st = (StudiesTypeDTO) it.next();
				sts[i] = st;
				i++;
			}
			
		} catch ( ConstraintViolationException e ) {
			logger.warn("ConstraintViolationException. " + e.getMessage());
		} catch ( Exception e ) {
			//System.out.println("EXCEPTION: Exception: " + e.getMessage());
		}
		
		CSStudiesTypeDTO[] csStudiesTypes = PlannerServiceUtil.convertFromStudiesTypeDTOToCSStudiesTypeDTO(sts);
		
		return csStudiesTypes;
	}

	public CSSubgroupTypeDTO[] getSubgroupTypes() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		SubgroupTypeDTO[] sts = new SubgroupTypeDTO[0];
		try {
			s.beginTransaction();
			List list = s.createQuery("from SubgroupTypeDTO").list();
			s.getTransaction().commit(); s.close();

			sts = new SubgroupTypeDTO[list.size()];
			int i=0;
			
			for (Iterator it = list.iterator(); it.hasNext();) {
				SubgroupTypeDTO st = (SubgroupTypeDTO) it.next();
				sts[i] = st;
				i++;
			}
			
		} catch ( ConstraintViolationException e ) {
			logger.warn("ConstraintViolationException. " + e.getMessage());
		} catch ( Exception e ) {
			//System.out.println("EXCEPTION: Exception: " + e.getMessage());
		}
		
		CSSubgroupTypeDTO[] subgroupTypes = PlannerServiceUtil.convertFromSubgroupTypeDTOToCSSubgroupTypeDTO(sts);
		
		return subgroupTypes;
	}
	
	
	public CSSubjectDTO[] getSubjects() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		SubjectDTO[] ss = new SubjectDTO[0];
		try {
			s.beginTransaction();
			List list = s.createQuery("from SubjectDTO").list();
			s.getTransaction().commit(); s.close();

			ss = new SubjectDTO[list.size()];
			int i=0;
			
			for (Iterator it = list.iterator(); it.hasNext();) {
				SubjectDTO su = (SubjectDTO) it.next();
				ss[i] = su;
				i++;
			}
			
		} catch ( ConstraintViolationException e ) {
			logger.warn("ConstraintViolationException. " + e.getMessage());
		} catch ( Exception e ) {
			//System.out.println("EXCEPTION: Exception: " + e.getMessage());
		}
		
		CSSubjectDTO[] csSubjects = PlannerServiceUtil.convertFromSubjectDTOToCSSubjectDTO(ss);

		return csSubjects;
	}

	
	public CSSubjectNameDTO[] getSubjectNames() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		SubjectNameDTO[] sns = new SubjectNameDTO[0];
		try {
			s.beginTransaction();
			List list = s.createQuery("from SubjectNameDTO").list();
			s.getTransaction().commit(); s.close();

			sns = new SubjectNameDTO[list.size()];
			int i=0;
			
			for (Iterator it = list.iterator(); it.hasNext();) {
				SubjectNameDTO sn = (SubjectNameDTO) it.next();
				sns[i] = sn;
				i++;
			}
			
		} catch ( ConstraintViolationException e ) {
			logger.warn("ConstraintViolationException. " + e.getMessage());
		} catch ( Exception e ) {
			logger.warn("Exception. " + e.getMessage());
		}
		
		CSSubjectNameDTO[] csSNS = PlannerServiceUtil.convertFromSubjectNameDTOToCSSubjectNameDTO(sns);
		
		return csSNS;
	}
	
	
	public CSSubjectTypeDTO[] getSubjectTypes() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		SubjectTypeDTO[] sts = new SubjectTypeDTO[0];
		try {
			s.beginTransaction();
			List list = s.createQuery("from SubjectTypeDTO").list();
			s.getTransaction().commit(); s.close();

			sts = new SubjectTypeDTO[list.size()];
			int i=0;
			
			for (Iterator it = list.iterator(); it.hasNext();) {
				SubjectTypeDTO st = (SubjectTypeDTO) it.next();
				sts[i] = st;
				i++;
			}
			
		} catch ( ConstraintViolationException e ) {
			logger.warn("ConstraintViolationException. " + e.getMessage());
		} catch ( Exception e ) {
			logger.warn("Exception. " + e.getMessage());
		}
		
		CSSubjectTypeDTO[] csSTS = PlannerServiceUtil.convertFromSubjectTypeDTOToCSSubjecttypeDTO(sts);
		
		return csSTS;
	}
	
	
	public CSRoomDTO[] getRooms() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		List list = s.createQuery("from RoomDTO").list();
		RoomDTO[] rooms = new RoomDTO[list.size()];
		
		int i=0;
		
		for (Iterator it = list.iterator(); it.hasNext();) {
			RoomDTO r = (RoomDTO) it.next();
			rooms[i] = r;
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		RoomDTO l = rooms[0];
		//s.evict(list);
		
		for (int j = 0; j < rooms.length; j++) {
			//System.out.println(j + ". " + rooms[j]);
		}
		//System.out.println("ROOM => " + l.getName() );
		//+ ", " + l.getSurname() + ", " + l.getAcademicTitle() + ", " + l.getAcademicTitle().getTitle());
		
		CSRoomDTO[] csRooms = PlannerServiceUtil.convertFromRoomDTOToCSRoomDTO(rooms);

		return csRooms;

		
//		LecturerDTO[] lecturers = new LecturerDTO[5];
//		for (int i = 0; i < lecturers.length; i++) {
//			LecturerDTO lecturer = new LecturerDTO();
//			int c = counter++;
//			lecturer.setName("firstName " + c);
//			lecturer.setSurname("surname " + c);
//			AcademicTitle at = new AcademicTitle("title " + c);
//			lecturer.setAcademicTitle(at);
//			lecturers[i] = lecturer;
//		}
//		
//		return lecturers; 
	}
	
	public CSLecturerDTO[] getLecturers() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		List list = s.createQuery("from LecturerDTO").list();
		LecturerDTO[] lecturers = new LecturerDTO[list.size()];
		
		int i=0;
		
		for (Iterator it = list.iterator(); it.hasNext();) {
			LecturerDTO l = (LecturerDTO) it.next();
			lecturers[i] = l;
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		LecturerDTO l = lecturers[0];
		//s.evict(list);
		
//		for (int j = 0; j < lecturers.length; j++) {
			//System.out.println(j + ". " + lecturers[j]);
//		}
		
		//System.out.println("LECTURER => " + l.getName() + ", " + l.getSurname() + ", " + l.getAcademicTitle() + ", " + l.getAcademicTitle().getTitle());
		
		CSLecturerDTO[] csLecturers = PlannerServiceUtil.convertFromLecturerDTOToCSLecturerDTO(lecturers);
		logger.debug("downloaded " + csLecturers.length + " lecturers");
		
		return csLecturers;

		
//		LecturerDTO[] lecturers = new LecturerDTO[5];
//		for (int i = 0; i < lecturers.length; i++) {
//			LecturerDTO lecturer = new LecturerDTO();
//			int c = counter++;
//			lecturer.setName("firstName " + c);
//			lecturer.setSurname("surname " + c);
//			AcademicTitle at = new AcademicTitle("title " + c);
//			lecturer.setAcademicTitle(at);
//			lecturers[i] = lecturer;
//		}
//		
//		return lecturers; 
	}
	
	public CSRoomTypeDTO[] getRoomTypes() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		RoomTypeDTO[] rts = new RoomTypeDTO[0];
		try {
			s.beginTransaction();
			List list = s.createQuery("from RoomTypeDTO").list();
			s.getTransaction().commit(); s.close();

			rts = new RoomTypeDTO[list.size()];
			int i=0;
			
			for (Iterator it = list.iterator(); it.hasNext();) {
				RoomTypeDTO rt = (RoomTypeDTO) it.next();
				rts[i] = rt;
				i++;
			}
			
		} catch ( ConstraintViolationException e ) {
			logger.warn("ConstraintViolationException. " + e.getMessage());
		} catch ( Exception e ) {
			logger.warn("Exception. " + e.getMessage());
		}
		
		CSRoomTypeDTO[] csRTs = PlannerServiceUtil.convertFromRoomTypeDTOToCSRoomDTO(rts);
		
		return csRTs;
	}
	
	public CSAcademicTitleDTO[] getAcademicTitles() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		AcademicTitleDTO[] ats = new AcademicTitleDTO[0];
		try {
			s.beginTransaction();
			List list = s.createQuery("from AcademicTitleDTO").list();
			s.getTransaction().commit(); s.close();

			ats = new AcademicTitleDTO[list.size()];
			int i=0;
			
			for (Iterator it = list.iterator(); it.hasNext();) {
				AcademicTitleDTO at = (AcademicTitleDTO) it.next();
				ats[i] = at;
				i++;
			}
			
		} catch ( ConstraintViolationException e ) {
			logger.warn("ConstraintViolationException. " + e.getMessage());
		} catch ( Exception e ) {
			logger.warn("Exception. " + e.getMessage());
		}

		CSAcademicTitleDTO[] csAcademicTitles = PlannerServiceUtil.convertFromAcademicTitleDTOToCSAcademicTitleDTO(ats);
		
		return csAcademicTitles;
	}
	
	public CSBuildingDTO[] getBuildings() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		BuildingDTO[] bs = new BuildingDTO[0];
		try {
			s.beginTransaction();
			List list = s.createQuery("from BuildingDTO").list();
			s.getTransaction().commit(); s.close();

			bs = new BuildingDTO[list.size()];
			int i=0;
			
			for (Iterator it = list.iterator(); it.hasNext();) {
				BuildingDTO at = (BuildingDTO) it.next();
				bs[i] = at;
				i++;
			}
			
			
		} catch ( ConstraintViolationException e ) {
			logger.warn("ConstraintViolationException. " + e.getMessage());
		} catch ( Exception e ) {
			logger.warn("Exception. " + e.getMessage());
		}
//		
//		try {
//			Thread.sleep(10000);
//		} catch (Exception e) {
//			System.out.println("WYWALILO WYAJTEK " + e);
//			// TODO: handle exception
//		}

		CSBuildingDTO[] csBs = PlannerServiceUtil.convertFromBuildingDTOToCSBuildingDTO(bs);
		
		return csBs;
	}
	
	
	
	public CSPreferenceTypeDTO[] getPreferenceTypes() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		PreferenceTypeDTO[] pts = new PreferenceTypeDTO[0];
		try {
			s.beginTransaction();
			List list = s.createQuery("from PreferenceTypeDTO").list();
			s.getTransaction().commit(); s.close();

			pts = new PreferenceTypeDTO[list.size()];
			int i=0;
			
			for (Iterator it = list.iterator(); it.hasNext();) {
				PreferenceTypeDTO pt = (PreferenceTypeDTO) it.next();
				pts[i] = pt;
				i++;
			}
			
		} catch ( ConstraintViolationException e ) {
			logger.warn("ConstraintViolationException. " + e.getMessage());
		} catch ( Exception e ) {
			logger.warn("Exception. " + e.getMessage());
		}
		
		CSPreferenceTypeDTO[] csPTS = PlannerServiceUtil.convertFromPreferenceTypeDTOToCSPreferenceTypeDTO(pts);
		
		return csPTS;
	}
	
	public CSPreferenceDTO[] getCSPreferencesBySemesterID(Long semesterID) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		PreferenceDTO[] preferences = null;
		
		try {
			s.beginTransaction();
			
			String q = 	"from PreferenceDTO p " +
			"where p.calendarDay.semester.id = :semesterID " +
			"order by p.calendarDay, p.startTime";
			
			Query query = s.createQuery(q).setLong("semesterID", semesterID);
			
			List<PreferenceDTO> list = query.list();
			preferences = list.toArray(new PreferenceDTO[0]);
			
			s.getTransaction().commit(); s.close();
			
			
			logger.info("downloaded " + preferences.length + " preferences for semester.id: " + semesterID);

			for (int i = 0; i < preferences.length; i++) {
				logger.debug(i + ". " + preferences[i].toString());
			}
			
		} catch (Exception e) {
			logger.warn("error while trying to download Preferences for semester.id: " + semesterID);
		}
		
		CSPreferenceDTO[] csPreferences = null;
		if (preferences.length > 0) {
			csPreferences = PlannerServiceUtil.convertFromPreferenceDTOToCSPreferenceDTO(preferences);
		}
		
		return csPreferences;
	}
	
	
	
	public AcademicTitleDTO getAcademicTitle() {
//		return "tytul";
		
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		AcademicTitleDTO list = (AcademicTitleDTO)s.createQuery("from AcademicTitleDTO where id=:id").setInteger("id",0).uniqueResult();
		s.getTransaction().commit(); s.close();
		return list;
	}
	
	
	/**
	 * Zapisuje badz aktualizuje obiekt SubjectEventDTO w bazie danych.
	 */
	public long saveCSSubjectEvent(CSSubjectEventDTO csSubjectEventDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSSubjectEvent(), subjectEvent: " + csSubjectEventDTO);
		
		SubjectEventDTO dto = null;
		
		if (csSubjectEventDTO.getId() <= 0) {
			dto = new SubjectEventDTO();
		} else {
			dto = (SubjectEventDTO)s.load(SubjectEventDTO.class, new Long(csSubjectEventDTO.getId()));
		}
		
		
		
		//subject
		long subjectID = csSubjectEventDTO.getSubjectID();
		if (subjectID == -1) {
			dto.setSubject(null);
		} else {
			SubjectDTO subj = (SubjectDTO)s.load(SubjectDTO.class, new Long(subjectID));
			dto.setSubject(subj);
		}
		
		
		//calendarDay
		long calendarDayID = csSubjectEventDTO.getCalendarDayID();
		if (calendarDayID == -1) {
			dto.setCalendarDay(null);
		} else {
			CalendarDayDTO cd = (CalendarDayDTO)s.load(CalendarDayDTO.class, new Long(calendarDayID));
			dto.setCalendarDay(cd);
		}
		
		
		//lecturer
		long lecturerID = csSubjectEventDTO.getLecturerID();
		if (lecturerID == -1) {
			dto.setLecturer(null);
		} else {
			LecturerDTO lect = (LecturerDTO)s.load(LecturerDTO.class, new Long(lecturerID)); 
			dto.setLecturer(lect);
		}
		
		
		//room
		long roomID = csSubjectEventDTO.getRoomID();
		if (roomID == -1) {
			dto.setRoom(null);
		} else {
			RoomDTO room = (RoomDTO)s.load(RoomDTO.class, new Long(roomID));
			dto.setRoom(room);
		}
		
		
		SubgroupDTO subgroup = (SubgroupDTO) s.load(SubgroupDTO.class, new Long(csSubjectEventDTO.getSubgroupID()));
		dto.setSubgroup(subgroup);
		
		dto.setEventSort(csSubjectEventDTO.getEventSort());
		dto.setStartTime(csSubjectEventDTO.getStartTime());
		dto.setDuration(csSubjectEventDTO.getDuration());

		
		
		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}
		return dto.getId();
	}
	
	/**
	 * Zapisuje badz aktualizuje tablice obiektow SubjectEventDTO w bazie danych.
	 */
	public long[] saveCSSubjectEvents(CSSubjectEventDTO[] csSubjectEventDTOs) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSSubjectEvents(), subjectEvent: " + csSubjectEventDTOs);
		
		long[] idki = new long[csSubjectEventDTOs.length];

		try {
			s.beginTransaction();
			List<SubjectEventDTO> seList = new ArrayList<SubjectEventDTO>();
			
			for (int i = 0; i < csSubjectEventDTOs.length; i++) {
				CSSubjectEventDTO csSubjectEventDTO = csSubjectEventDTOs[i];
				
				SubjectEventDTO dto = null;
				
				if (csSubjectEventDTO.getId() <= 0) {
					dto = new SubjectEventDTO();
				} else {
					dto = (SubjectEventDTO)s.load(SubjectEventDTO.class, new Long(csSubjectEventDTO.getId()));
				}
				
				
				//subject
				long subjectID = csSubjectEventDTO.getSubjectID();
				if (subjectID == -1) {
					dto.setSubject(null);
				} else {
					SubjectDTO subj = (SubjectDTO)s.load(SubjectDTO.class, new Long(subjectID));
					dto.setSubject(subj);
				}
				
				
				//calendarDay
				long calendarDayID = csSubjectEventDTO.getCalendarDayID();
				if (calendarDayID == -1) {
					dto.setCalendarDay(null);
				} else {
					CalendarDayDTO cd = (CalendarDayDTO)s.load(CalendarDayDTO.class, new Long(calendarDayID));
					dto.setCalendarDay(cd);
				}
				
				
				//lecturer
				long lecturerID = csSubjectEventDTO.getLecturerID();
				if (lecturerID == -1) {
					dto.setLecturer(null);
				} else {
					LecturerDTO lect = (LecturerDTO)s.load(LecturerDTO.class, new Long(lecturerID)); 
					dto.setLecturer(lect);
				}
				
				
				//room
				long roomID = csSubjectEventDTO.getRoomID();
				if (roomID == -1) {
					dto.setRoom(null);
				} else {
					RoomDTO room = (RoomDTO)s.load(RoomDTO.class, new Long(roomID));
					dto.setRoom(room);
				}
				
				
				SubgroupDTO subgroup = (SubgroupDTO) s.load(SubgroupDTO.class, new Long(csSubjectEventDTO.getSubgroupID()));
				dto.setSubgroup(subgroup);
				
				dto.setEventSort(csSubjectEventDTO.getEventSort());
				dto.setStartTime(csSubjectEventDTO.getStartTime());
				dto.setDuration(csSubjectEventDTO.getDuration());

				s.saveOrUpdate(dto);
				seList.add(dto);
			}

			
			s.getTransaction().commit();
			
			for (int j = 0; j < idki.length; j++) {
				idki[j] = seList.get(j).getId();
			}
			s.close();

		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}

			
		
		return idki;
	}
	
	

	
	public boolean removeCSSubjectEvent(CSSubjectEventDTO subjectEvent) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSSubjectEvent(), subjectEvent: " + subjectEvent);
		
		
		SubjectEventDTO se = (SubjectEventDTO)s.load(SubjectEventDTO.class, new Long(subjectEvent.getId()));

		try {
			s.beginTransaction();
			s.delete(se);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}
	
	/**
	 * Zapisuje badz aktualizuje obiekt DeclarationDTO w bazie danych.
	 */
	public long saveCSDeclaration(CSDeclarationDTO csDeclarationDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSDeclaration(), declaration: " + csDeclarationDTO);
		
		DeclarationDTO dto = null;
		
		if (csDeclarationDTO.getId() <= 0) {
			dto = new DeclarationDTO();
		} else {
			dto = (DeclarationDTO)s.load(DeclarationDTO.class, new Long(csDeclarationDTO.getId()));
		}
		
		
		GroupDTO group =  (GroupDTO) s.load(GroupDTO.class, new Long(csDeclarationDTO.getGroupID()));
		dto.setGroup(group);
		
		SubgroupTypeDTO subgroupType = (SubgroupTypeDTO) s.load(SubgroupTypeDTO.class, new Long(csDeclarationDTO.getSubgroupTypeID()));
		dto.setSubgroupType(subgroupType);
		
		SubjectDTO subject = (SubjectDTO) s.load(SubjectDTO.class, new Long(csDeclarationDTO.getSubjectID()));
		dto.setSubject(subject);
		
		
//		dto.setSubgroupsAmount(csDeclarationDTO.getSubgroupsAmount());
		dto.setDuration(csDeclarationDTO.getDuration());

		
		
		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}
		return dto.getId();
	}

	
	public boolean removeCSDeclaration(CSDeclarationDTO declaration) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSDeclaration(), declaration: " + declaration);
		
		
		DeclarationDTO se = (DeclarationDTO)s.load(DeclarationDTO.class, new Long(declaration.getId()));

		try {
			s.beginTransaction();
			s.delete(se);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}
	
	/**
	 * Zapisuje badz aktualizuje obiekt LecturerDTO w bazie danych.
	 */
	public long saveCSLecturer(CSLecturerDTO csLecturerDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSLecturer(), lecturer: " + csLecturerDTO);
		
		LecturerDTO dto = null;
		
		if (csLecturerDTO.getId() <= 0) {
			dto = new LecturerDTO();
		} else {
			dto = (LecturerDTO)s.load(LecturerDTO.class, new Long(csLecturerDTO.getId()));
		}
		
		
		//academicTitle
		AcademicTitleDTO at = (AcademicTitleDTO)s.load(AcademicTitleDTO.class, new Long(csLecturerDTO.getAcademicTitleID()));
		dto.setAcademicTitle(at);

		dto.setName(csLecturerDTO.getName());
		dto.setSurname(csLecturerDTO.getSurname());
		
		long id = 0;
		
		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
			logger.info("Lecturer saved, id: " + dto.getId() + ", surname: " + dto.getSurname());
			id = dto.getId();
		} catch ( HibernateException e ) {
			logger.warn("Lecturer NOT saved, id: " + dto.getId() + ", " + dto );
		}
		return id;
		
	}
	
	public boolean removeCSLecturer(CSLecturerDTO lecturer) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSLecturer(), lecturer: " + lecturer);
		
		
		LecturerDTO l = (LecturerDTO)s.load(LecturerDTO.class, new Long(lecturer.getId()));

		try {
			s.beginTransaction();
			s.delete(l);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}

	/**
	 * Zapisuje badz aktualizuje obiekt SemesterDTO w bazie danych.
	 */
	public long saveCSSemester(CSSemesterDTO csSemesterDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSSemester(), semester: " + csSemesterDTO);
		
		SemesterDTO dto = null;
		
		if (csSemesterDTO.getId() <= 0) {
			dto = new SemesterDTO();
		} else {
			dto = (SemesterDTO)s.load(SemesterDTO.class, new Long(csSemesterDTO.getId()));
		}
		
		//studiesType
		StudiesTypeDTO st = (StudiesTypeDTO)s.load(StudiesTypeDTO.class, new Long(csSemesterDTO.getStudiesTypeID()));
		dto.setStudiesType(st);

		//semesterType
		SemesterTypeDTO semt = (SemesterTypeDTO)s.load(SemesterTypeDTO.class, new Long(csSemesterDTO.getSemesterTypeID()));
		dto.setSemesterType(semt);
		
		//yearNumber
		dto.setYearNumber(csSemesterDTO.getYearNumber());
		
		//visibleForOrdinaryUsers
		dto.setVisibleForOrdinaryUsers(csSemesterDTO.isVisibleForOrdinaryUsers());

		
		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}
		return dto.getId();
	}

	
	/**
	 * Zapisuje w bazie danych nowy obiekt CSSemesterDTO stworzony na podstawie istniejacego semestru.
	 */
	/* (non-Javadoc)
	 * @see pl.prv.polanie.client.service.PlannerService#createSemesterBasingOnExistingOne(pl.prv.polanie.client.dto.CSSemesterDTO, pl.prv.polanie.client.dto.CSSemesterDTO)
	 */
	public CSTDuplicatingSemesterReportDTO createSemesterBasingOnExistingOne(CSSemesterDTO newSemester, CSSemesterDTO existingS) {
		

//		TODO
//			tworzenie nowego semestru w oparciu na istniejacym
//			(backup bazy i jazda)
//
//		wrzucic wszystko do transakcji
//			
//		1. stworzyc nowy semester
//			w ustawieniach semestru pozostaje takie samo:
//			-studies type
//
//		2. skopiowac:
//			- calendar days,
//			- preferencje,
//
//			- semester year groups
//			- groups
//			- subgroups
//			- declarations
//			- subject events
		if (newSemester.getId() > 0) {
			logger.warn("Incorrect use of method - existing semester given as parameter.");
		}
		
		CSTDuplicatingSemesterReportDTO dsrDTO = new CSTDuplicatingSemesterReportDTO();

		
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
//		SemesterDTO existingDTO = (SemesterDTO)s.load(SemesterDTO.class, new Long(existingS.getId()));
		
		SemesterDTO newSemesterDTO = new SemesterDTO();
	
		//studiesType
		StudiesTypeDTO st = (StudiesTypeDTO)s.load(StudiesTypeDTO.class, new Long(existingS.getStudiesTypeID()));
		newSemesterDTO.setStudiesType(st);

		//semesterType
		SemesterTypeDTO semt = (SemesterTypeDTO)s.load(SemesterTypeDTO.class, new Long(existingS.getSemesterTypeID()));
		newSemesterDTO.setSemesterType(semt);
		
		//yearNumber
		//		YearNumberDTO yn = (YearNumberDTO)s.load(YearNumberDTO.class, new Long(existingS.getYearNumberID()));
		newSemesterDTO.setYearNumber(existingS.getYearNumber());
		
		
		try {
			s.beginTransaction();
			
			s.saveOrUpdate(newSemesterDTO);

			//CALENDAR DAYS
			//wczytac wszystkie calendardays z tamtego semestru
			//stworzyc takie same ale z nowym semesterID
			
			String q = 	"from CalendarDayDTO cd " +
				"where cd.semester.id = :semesterID";

			Query query = s.createQuery(q).setLong("semesterID", new Long(existingS.getId()));
			List<CalendarDayDTO> calendarDaysList = query.list();
			
			//mapa <id istniejacego cd, id stworzonego cd> 
			Map<Long, Long> cdsIDs = new HashMap<Long, Long>();
			
			for (int i = 0; i < calendarDaysList.size(); i++) {
				CalendarDayDTO cd = calendarDaysList.get(i);
				
				CalendarDayDTO newCD = new CalendarDayDTO();
				newCD.setDayLabel(cd.getDayLabel());
				newCD.setDisplayOrder(cd.getDisplayOrder());
				newCD.setSemester(newSemesterDTO);
				newCD.setSunday(cd.isSunday());
				
				s.saveOrUpdate(newCD);
				
				cdsIDs.put(new Long(cd.getId()), new Long(newCD.getId()));
				logger.debug("Creating " + (i+1) + ". day attached to just created Semester, id:" + newCD.getId());
			}
			
			
			
			//PREFERENCES
			//wczytac wszystkie preferences z tamtego semestru
			//stworzyc takie same ale z nowymi calendarID
			String preferencesQ = "from PreferenceDTO p " +
					"where p.calendarDay.semester.id = :semesterID";

			Query preferencesQuery = s.createQuery(preferencesQ).setLong("semesterID", new Long(existingS.getId()));
			List<PreferenceDTO> preferencesList = preferencesQuery.list();
			
			//mapa <id istniejacego pref, id stworzonego pref> 
//			Map<Long, Long> prefsIDs = new HashMap<Long, Long>();
			
			for (int i = 0; i < preferencesList.size(); i++) {
				PreferenceDTO pref = preferencesList.get(i);
				
				PreferenceDTO newP = new PreferenceDTO();
				
				Long createdCDID = cdsIDs.get(new Long(pref.getCalendarDay().getId()));
				CalendarDayDTO cd = (CalendarDayDTO)s.load(CalendarDayDTO.class, createdCDID);
				newP.setCalendarDay(cd);
				newP.setDuration(new Integer(pref.getDuration()));
				newP.setLecturer(pref.getLecturer());
				newP.setPreferenceType(pref.getPreferenceType());
				newP.setStartTime(pref.getStartTime());
				
				s.saveOrUpdate(newP);
				
				logger.debug("Creating " + (i+1) + ". preference attached to just created day, id:" + createdCDID);
			}
			
			
			
			//SEMESTER YEAR GROUPS
			//wczytac wszystkie semester year groups z tamtego semestru
			//stworzyc takie same ale z nowym semesterID
			
			String sygQ = 	"from SemestersYearGroupsDTO syg " +
				"where syg.semester.id = :semesterID";

			Query sygQuery = s.createQuery(sygQ).setLong("semesterID", new Long(existingS.getId()));
			List<SemestersYearGroupsDTO> semestersYearGroupsList = sygQuery.list();
			
			//mapa <id istniejacego syg, id stworzonego syg> 
			Map<Long, Long> sygsIDs = new HashMap<Long, Long>();
			
			for (int i = 0; i < semestersYearGroupsList.size(); i++) {
				SemestersYearGroupsDTO syg = semestersYearGroupsList.get(i);
				
				SemestersYearGroupsDTO newSYG = new SemestersYearGroupsDTO();
				newSYG.setSemester(newSemesterDTO);
				newSYG.setStudentsNumber(syg.getStudentsNumber());
				newSYG.setYearGroupName(syg.getYearGroupName());
				
				s.saveOrUpdate(newSYG);
				
				sygsIDs.put(new Long(syg.getId()), new Long(newSYG.getId()));
				logger.debug("Creating " + (i+1) + ". SYG attached to just created Semester, id:" + newSYG.getId());
			}
			
			
			
			//GROUPS
			//wczytac wszystkie groups z tamtego semestru
			//stworzyc takie same ale z nowymi sygID
			String groupsQ = "from GroupDTO g " +
					"where g.semestersYearGroup.semester.id = :semesterID";

			Query groupsQuery = s.createQuery(groupsQ).setLong("semesterID", new Long(existingS.getId()));
			List<GroupDTO> groupsList = groupsQuery.list();
			
			//mapa <id istniejacej group, id stworzonego group> 
			Map<Long, Long> gsIDs = new HashMap<Long, Long>();
			
			for (int i = 0; i < groupsList.size(); i++) {
				GroupDTO gr = groupsList.get(i);
				
				GroupDTO newG = new GroupDTO();
				
				newG.setName(gr.getName());
				newG.setScheduleProgress(gr.getScheduleProgress());
				
				Long createdSYGID = sygsIDs.get(new Long(gr.getSemestersYearGroup().getId()));
				SemestersYearGroupsDTO syg = (SemestersYearGroupsDTO)s.load(SemestersYearGroupsDTO.class, createdSYGID);
				newG.setSemestersYearGroup(syg);
				
				newG.setStudentsNumber(gr.getStudentsNumber());
				
				s.saveOrUpdate(newG);
				gsIDs.put(new Long(gr.getId()), new Long(newG.getId()));
				logger.debug("Creating " + (i+1) + ". group attached to just created SYG, id:" + createdSYGID);
			}
			
			
			
			//SUBGROUPS
			//wczytac wszystkie subgroups z tamtego semestru
			//stworzyc takie same ale z nowymi gID
			String subgroupsQ = "from SubgroupDTO sg " +
					"where sg.group.semestersYearGroup.semester.id = :semesterID";

			Query subgroupsQuery = s.createQuery(subgroupsQ).setLong("semesterID", new Long(existingS.getId()));
			List<SubgroupDTO> subgroupsList = subgroupsQuery.list();
			
			//mapa <id istniejacej subgorup, id stworzonego subgroup> 
			Map<Long, Long> sgsIDs = new HashMap<Long, Long>();
			
			for (int i = 0; i < subgroupsList.size(); i++) {
				SubgroupDTO sgr = subgroupsList.get(i);
				
				SubgroupDTO newS = new SubgroupDTO();
				
				Long createdGID = gsIDs.get(new Long(sgr.getGroup().getId()));
				GroupDTO g = (GroupDTO)s.load(GroupDTO.class, createdGID);
				newS.setGroup(g);
				
				newS.setName(sgr.getName());
				newS.setStudentsNumber(sgr.getStudentsNumber());
				newS.setSubgroupType(sgr.getSubgroupType());
				
				s.saveOrUpdate(newS);
				sgsIDs.put(new Long(sgr.getId()), new Long(newS.getId()));
				logger.debug("Creating " + (i+1) + ". subgroup attached to just created Group, id:" + createdGID);
			}
			
			
			//DECLARATIONS
			//wczytac wszystkie declarations z tamtego semestru
			//stworzyc takie same ale z nowymi gID
			String declarationsQ = "from DeclarationDTO d " +
					"where d.group.semestersYearGroup.semester.id = :semesterID";

			Query declarationsQuery = s.createQuery(declarationsQ).setLong("semesterID", new Long(existingS.getId()));
			List<DeclarationDTO> declarationsList = declarationsQuery.list();
			
//			//mapa <id istniejacej subgorup, id stworzonego subgroup> 
//			Map<Long, Long> sgsIDs = new HashMap<Long, Long>();
			
			for (int i = 0; i < declarationsList.size(); i++) {
				DeclarationDTO d = declarationsList.get(i);
				
				DeclarationDTO newD = new DeclarationDTO();
				
				newD.setDuration(d.getDuration());
				
				Long createdGID = gsIDs.get(new Long(d.getGroup().getId()));
				GroupDTO g = (GroupDTO)s.load(GroupDTO.class, createdGID);
				newD.setGroup(g);
				
				newD.setSubgroupType(d.getSubgroupType());
				newD.setSubject(d.getSubject());
				
				s.saveOrUpdate(newD);
//				sgsIDs.put(new Long(sgr.getId()), new Long(newS.getId()));
				logger.debug("Creating " + (i+1) + ". declaration attached to just created Group, id:" + createdGID);
			}
			
			
			//SUBJECT EVENTS
			//wczytac wszystkie subject events z tamtego semestru
			//stworzyc takie same ale z nowymi cdID, subgroupID, gID
			String subjectEventsQ = "from SubjectEventDTO se " +
					"where se.calendarDay.semester.id = :semesterID";

			Query subjectEventsQuery = s.createQuery(subjectEventsQ).setLong("semesterID", new Long(existingS.getId()));
			List<SubjectEventDTO> subjectEventsList = subjectEventsQuery.list();
			
//			//mapa <id istniejacej subgorup, id stworzonego subgroup> 
//			Map<Long, Long> sgsIDs = new HashMap<Long, Long>();
			
			for (int i = 0; i < subjectEventsList.size(); i++) {
				SubjectEventDTO se = subjectEventsList.get(i);
				
				SubjectEventDTO newSE = new SubjectEventDTO();

				Long createdCDID = cdsIDs.get(new Long(se.getCalendarDay().getId()));
				CalendarDayDTO cd = (CalendarDayDTO)s.load(CalendarDayDTO.class, createdCDID);
				newSE.setCalendarDay(cd);

				newSE.setDuration(se.getDuration());
				newSE.setEventSort(se.getEventSort());
				newSE.setLecturer(se.getLecturer());
				newSE.setRoom(se.getRoom());
				newSE.setStartTime(se.getStartTime());
				
				Long createdSGID = sgsIDs.get(new Long(se.getSubgroup().getId()));
				SubgroupDTO sg = (SubgroupDTO)s.load(SubgroupDTO.class, createdSGID);
				newSE.setSubgroup(sg);

				newSE.setSubject(se.getSubject());
				
				s.saveOrUpdate(newSE);
//				sgsIDs.put(new Long(sgr.getId()), new Long(newS.getId()));
				logger.debug("Creating " + i + " subject event attached to just created Subgroup, id:" + createdSGID + ", and CD, id: " + createdCDID);
			}
			
			
//			s.saveOrUpdate(existingDTO);
			dsrDTO.setCreatedCalendarDaysQuantity(calendarDaysList.size());
			dsrDTO.setCreatedDeclarationsQuantity(declarationsList.size());
			dsrDTO.setCreatedGroupsQuantity(groupsList.size());
			dsrDTO.setCreatedPreferencesQuantity(preferencesList.size());
			dsrDTO.setCreatedSemesterID(newSemesterDTO.getId());
			dsrDTO.setCreatedSemesterYearGroupsQuantity(semestersYearGroupsList.size());
			dsrDTO.setCreatedSubgroupsQuantity(subgroupsList.size());
			dsrDTO.setCreatedSubjectEventsQuantity(subjectEventsList.size());
			
			
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			logger.warn("Error while trying to create new Semester basing on existing one.");
			//System.out.println("blad > " + e.toString());
		}
		return dsrDTO;
	}

	
	public boolean removeCSSemester(CSSemesterDTO semester) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSSemester(), semester: " + semester);
		
		
		SemesterDTO l = (SemesterDTO)s.load(SemesterDTO.class, new Long(semester.getId()));

		try {
			s.beginTransaction();
			s.delete(l);
			s.getTransaction().commit(); s.close();
			logger.info("Semester with id " + l.getId() + " removed.");
			return true;
		} catch ( HibernateException e ) {
			logger.warn("Error while trying to delete semester with id: " + l.getId() + ", e: " + e.getMessage());
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * Zapisuje badz aktualizuje obiekt SubgroupDTO w bazie danych.
	 */
	public long saveCSSubgroup(CSSubgroupDTO csSubgroupDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSSubgroup(), subgroup: " + csSubgroupDTO);
		
		SubgroupDTO dto = null;
		
		if (csSubgroupDTO.getId() <= 0) {
			dto = new SubgroupDTO();
		} else {
			dto = (SubgroupDTO)s.load(SubgroupDTO.class, new Long(csSubgroupDTO.getId()));
		}
		
		
		
		
		//subgroupType
		SubgroupTypeDTO st = (SubgroupTypeDTO)s.load(SubgroupTypeDTO.class, new Long(csSubgroupDTO.getSubgroupTypeID()));
		dto.setSubgroupType(st);
		
		//group
		GroupDTO g = (GroupDTO)s.load(GroupDTO.class, new Long(csSubgroupDTO.getGroupID()));
		dto.setGroup(g);
		
		
		//
		dto.setName(csSubgroupDTO.getName());
		dto.setStudentsNumber(csSubgroupDTO.getStudentsNumber());
		

		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}
		return dto.getId();
	}
	

	public boolean removeCSSubgroup(CSSubgroupDTO subgroup) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSSubgroup(), subgroup: " + subgroup);
		
		
		SubgroupDTO sg = (SubgroupDTO)s.load(SubgroupDTO.class, new Long(subgroup.getId()));

		try {
			s.beginTransaction();
			s.delete(sg);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}
	
	/**
	 * Zapisuje badz aktualizuje obiekt GroupDTO w bazie danych.
	 */
	public long saveCSGroup(CSGroupDTO csGroupDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSGroup(), group: " + csGroupDTO);
		
		GroupDTO dto = null;
		
		if (csGroupDTO.getId() <= 0) {
			dto = new GroupDTO();
		} else {
			dto = (GroupDTO)s.load(GroupDTO.class, new Long(csGroupDTO.getId()));
		}
		

		//syg
		SemestersYearGroupsDTO syg = (SemestersYearGroupsDTO)s.load(SemestersYearGroupsDTO.class, new Long(csGroupDTO.getSemestersYearGroupID()));
		dto.setSemestersYearGroup(syg);
		
		//
		dto.setName(csGroupDTO.getName());
		dto.setStudentsNumber(csGroupDTO.getStudentsNumber());
		dto.setScheduleProgress(csGroupDTO.getScheduleProgress());
		

		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}
		return dto.getId();
	}
	
	public boolean removeCSGroup(CSGroupDTO group) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSGroup(), group: " + group);
		
		
		GroupDTO g = (GroupDTO)s.load(GroupDTO.class, new Long(group.getId()));

		try {
			s.beginTransaction();
			s.delete(g);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}
	
	
	
	
	/**
	 * Zapisuje badz aktualizuje obiekt LecturerDTO w bazie danych.
	 */
	public long saveCSAcademicTitle(CSAcademicTitleDTO csAcademicTitleDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSAcademicTitle(), acad title: " + csAcademicTitleDTO);
		
		AcademicTitleDTO dto = null;
		
		if (csAcademicTitleDTO.getId() <= 0) {
			dto = new AcademicTitleDTO();
		} else {
			dto = (AcademicTitleDTO)s.load(AcademicTitleDTO.class, new Long(csAcademicTitleDTO.getId()));
		}
		
		
		dto.setTitle(csAcademicTitleDTO.getTitle());

		long id = 0;
		
		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
			logger.warn("Academic Title saved, id: " + dto.getId() + ", title: " + dto.getTitle());
			id = dto.getId();
		} catch ( HibernateException e ) {
			logger.warn("Academic Title not saved, id: " + dto.getId() + ", title: " + dto.getTitle());
		}
		return id;
	}
	
	public boolean removeCSAcademicTitle(CSAcademicTitleDTO academicTitle) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSacademicTitle(), lecturer: " + academicTitle);
		
		
		AcademicTitleDTO at = (AcademicTitleDTO)s.load(AcademicTitleDTO.class, new Long(academicTitle.getId()));

		try {
			s.beginTransaction();
			s.delete(at);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}
	
	
	/**
	 * Zapisuje badz aktualizuje obiekt LecturerDTO w bazie danych.
	 */
	public long saveCSPreferenceType(CSPreferenceTypeDTO csPreferenceTypeDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSPreferenceType(), acad title: " + csPreferenceTypeDTO);
		
		PreferenceTypeDTO dto = null;
		
		if (csPreferenceTypeDTO.getId() <= 0) {
			dto = new PreferenceTypeDTO();
		} else {
			dto = (PreferenceTypeDTO)s.load(PreferenceTypeDTO.class, new Long(csPreferenceTypeDTO.getId()));
		}
		
		
		dto.setName(csPreferenceTypeDTO.getName());
		dto.setColor(csPreferenceTypeDTO.getColor());

		
		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}
		return dto.getId();
	}
	
	public boolean removeCSPreferenceType(CSPreferenceTypeDTO preferenceType) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSacademicTitle(), lecturer: " + academicTitle);
		
		
		PreferenceTypeDTO pt = (PreferenceTypeDTO)s.load(PreferenceTypeDTO.class, new Long(preferenceType.getId()));

		try {
			s.beginTransaction();
			s.delete(pt);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}
	
	
	/**
	 * Zapisuje badz aktualizuje obiekt CalendarDayDTO w bazie danych.
	 */
	public long saveCSCalendarDay(CSCalendarDayDTO csCalendarDayDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSCalendarDay(), cd: " + csCalendarDayDTO);
		
		CalendarDayDTO dto = null;
		
		if (csCalendarDayDTO.getId() <= 0) {
			dto = new CalendarDayDTO();
		} else {
			dto = (CalendarDayDTO)s.load(CalendarDayDTO.class, new Long(csCalendarDayDTO.getId()));
		}
		
		
		dto.setDayLabel(csCalendarDayDTO.getDayLabel());
		dto.setDisplayOrder(csCalendarDayDTO.getDisplayOrder());
		dto.setSunday(csCalendarDayDTO.isSunday());
		
		
		//semester
		SemesterDTO sem = (SemesterDTO)s.load(SemesterDTO.class, new Long(csCalendarDayDTO.getSemesterID()));
		dto.setSemester(sem);
		
		//System.out.println("XXXXXXX " + dto);
		
		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}
		return dto.getId();
	}
	
	public boolean removeCSCalendarDay(CSCalendarDayDTO calendarDay) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSCalendarDay(), cd: " + calendarDay);
		
		
		CalendarDayDTO cd = (CalendarDayDTO)s.load(CalendarDayDTO.class, new Long(calendarDay.getId()));

		try {
			s.beginTransaction();
			s.delete(cd);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}
	
	
	/**
	 * Zapisuje badz aktualizuje obiekt SubjectNameDTO w bazie danych.
	 */
	public long saveCSSubjectName(CSSubjectNameDTO csSubjectNameDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSSubjectName(), sn: " + csSubjectNameDTO);
		
		SubjectNameDTO dto = null;
		
		if (csSubjectNameDTO.getId() <= 0) {
			dto = new SubjectNameDTO();
		} else {
			dto = (SubjectNameDTO)s.load(SubjectNameDTO.class, new Long(csSubjectNameDTO.getId()));
		}
		
		
		dto.setName(csSubjectNameDTO.getName());
		dto.setSymbol(csSubjectNameDTO.getSymbol());
		
		
		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}
		return dto.getId();
	}
	
	public boolean removeCSSubjectName(CSSubjectNameDTO subjectName) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSSubjectName(), cd: " + subjectName);
		
		
		SubjectNameDTO sn = (SubjectNameDTO)s.load(SubjectNameDTO.class, new Long(subjectName.getId()));

		try {
			s.beginTransaction();
			s.delete(sn);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}
	
	
	
	/**
	 * Zapisuje badz aktualizuje obiekt SubjectDTO w bazie danych.
	 */
	public long saveCSSubject(CSSubjectDTO csSubjectDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSSubject(), subject: " + csSubjectDTO);
		
		SubjectDTO dto = null;
		
		if (csSubjectDTO.getId() <= 0) {
			dto = new SubjectDTO();
		} else {
			dto = (SubjectDTO)s.load(SubjectDTO.class, new Long(csSubjectDTO.getId()));
		}
		
		
		
		
		
		//subjectType
		SubjectTypeDTO st = (SubjectTypeDTO)s.load(SubjectTypeDTO.class, new Long(csSubjectDTO.getSubjectTypeID()));
		dto.setSubjectType(st);

		//subjectName
		SubjectNameDTO sn = (SubjectNameDTO)s.load(SubjectNameDTO.class, new Long(csSubjectDTO.getSubjectNameID()));
		dto.setSubjectName(sn);

		
		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}
		return dto.getId();
	}
	
	public boolean removeCSSubject(CSSubjectDTO subject) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSSubject(), lecturer: " + subject);
		
		
		SubjectDTO l = (SubjectDTO)s.load(SubjectDTO.class, new Long(subject.getId()));

		try {
			s.beginTransaction();
			s.delete(l);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}

	/**
	 * Zapisuje badz aktualizuje obiekt SubjectTypeDTO w bazie danych.
	 */
	public long saveCSSubjectType(CSSubjectTypeDTO csSubjectTypeDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSSubjectType(), st: " + csSubjectTypeDTO);
		
		SubjectTypeDTO dto = null;
		
		if (csSubjectTypeDTO.getId() <= 0) {
			dto = new SubjectTypeDTO();
		} else {
			dto = (SubjectTypeDTO)s.load(SubjectTypeDTO.class, new Long(csSubjectTypeDTO.getId()));
		}
		
		
		dto.setName(csSubjectTypeDTO.getName());
		dto.setInitial(csSubjectTypeDTO.getInitial());
		
		
		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}
		return dto.getId();
	}
	
	public boolean removeCSSubjectType(CSSubjectTypeDTO subjectType) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSSubjectType(), cd: " + subjectType);
		
		
		SubjectTypeDTO st = (SubjectTypeDTO)s.load(SubjectTypeDTO.class, new Long(subjectType.getId()));

		try {
			s.beginTransaction();
			s.delete(st);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}
	
	
	/**
	 * Zapisuje badz aktualizuje obiekt SubgroupTypeDTO w bazie danych.
	 */
	public long saveCSSubgroupType(CSSubgroupTypeDTO csSubgroupTypeDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSSubgroupType(), st: " + csSubgroupTypeDTO);
		
		SubgroupTypeDTO dto = null;
		
		if (csSubgroupTypeDTO.getId() <= 0) {
			dto = new SubgroupTypeDTO();
		} else {
			dto = (SubgroupTypeDTO)s.load(SubgroupTypeDTO.class, new Long(csSubgroupTypeDTO.getId()));
		}
		
		
		dto.setName(csSubgroupTypeDTO.getName());
		
		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString() + ", DTO >>> " + dto);
		}
		return dto.getId();
	}
	
	public boolean removeCSSubgroupType(CSSubgroupTypeDTO subjectType) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSSubgroupType(), cd: " + subjectType);
		
		
		SubgroupTypeDTO st = (SubgroupTypeDTO)s.load(SubgroupTypeDTO.class, new Long(subjectType.getId()));

		try {
			s.beginTransaction();
			s.delete(st);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}
	
	
	/**
	 * Zapisuje badz aktualizuje obiekt BuildingDTO w bazie danych.
	 */
	public long saveCSBuilding(CSBuildingDTO csBuildingDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSBuilding(), st: " + csBuildingDTO);
		
		BuildingDTO dto = null;
		
		if (csBuildingDTO.getId() <= 0) {
			dto = new BuildingDTO();
		} else {
			dto = (BuildingDTO)s.load(BuildingDTO.class, new Long(csBuildingDTO.getId()));
		}
		
		
		dto.setName(csBuildingDTO.getName());
		dto.setSymbol(csBuildingDTO.getSymbol());

		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}
		return dto.getId();
	}
	
	public boolean removeCSBuilding(CSBuildingDTO building) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSBuilding(), cd: " + building);
		
		
		BuildingDTO b = (BuildingDTO)s.load(BuildingDTO.class, new Long(building.getId()));

		try {
			s.beginTransaction();
			s.delete(b);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}
	
	/**
	 * Zapisuje badz aktualizuje obiekt RoomTypeDTO w bazie danych.
	 */
	public long saveCSRoomType(CSRoomTypeDTO csRoomTypeDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSRoomType(), r: " + csRoomTypeDTO);
		
		RoomTypeDTO dto = null;
		
		if (csRoomTypeDTO.getId() <= 0) {
			dto = new RoomTypeDTO();
		} else {
			dto = (RoomTypeDTO)s.load(RoomTypeDTO.class, new Long(csRoomTypeDTO.getId()));
		}
		
		dto.setName(csRoomTypeDTO.getName());

		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}
		return dto.getId();
	}
	
	public boolean removeCSRoomType(CSRoomTypeDTO roomType) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSRoomType(), cd: " + roomType);
		
		
		RoomTypeDTO b = (RoomTypeDTO)s.load(RoomTypeDTO.class, new Long(roomType.getId()));

		try {
			s.beginTransaction();
			s.delete(b);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}
	
	
	
	/**
	 * Zapisuje badz aktualizuje obiekt RoomDTO w bazie danych.
	 */
	public long saveCSRoom(CSRoomDTO csRoomDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSRoom(), r: " + csRoomDTO);
		
		RoomDTO dto = null;
		
		if (csRoomDTO.getId() <= 0) {
			dto = new RoomDTO();
		} else {
			dto = (RoomDTO)s.load(RoomDTO.class, new Long(csRoomDTO.getId()));
		}
		
		//building
		BuildingDTO b = (BuildingDTO)s.load(BuildingDTO.class, new Long(csRoomDTO.getBuildingID()));
		dto.setBuilding(b);

		//roomType
		RoomTypeDTO rt = (RoomTypeDTO)s.load(RoomTypeDTO.class, new Long(csRoomDTO.getRoomTypeID()));
		dto.setRoomType(rt);
		
		
		dto.setName(csRoomDTO.getName());
		dto.setCapacity(csRoomDTO.getCapacity());

		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}
		return dto.getId();
	}
	
	public boolean removeCSRoom(CSRoomDTO room) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSRoom(), cd: " + room);
		
		
		RoomDTO b = (RoomDTO)s.load(RoomDTO.class, new Long(room.getId()));

		try {
			s.beginTransaction();
			s.delete(b);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}
	
	
	/**
	 * Zapisuje badz aktualizuje obiekt YearGroupNameDTO w bazie danych.
	 */
	public long saveCSYearGroupName(CSYearGroupNameDTO csYearGroupNameDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSYearGroupName(), name: " + csYearGroupNameDTO);
		
		YearGroupNameDTO dto = null;
		
		if (csYearGroupNameDTO.getId() <= 0) {
			dto = new YearGroupNameDTO();
		} else {
			dto = (YearGroupNameDTO)s.load(YearGroupNameDTO.class, new Long(csYearGroupNameDTO.getId()));
		}
		
		
		dto.setName(csYearGroupNameDTO.getName());

		
		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}
		return dto.getId();
	}
	
	public boolean removeCSYearGroupName(CSYearGroupNameDTO yearGroupName) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSYearGroupName(), ygn: " + yearGroupName);
		
		
		YearGroupNameDTO ygn = (YearGroupNameDTO)s.load(YearGroupNameDTO.class, new Long(yearGroupName.getId()));

		try {
			s.beginTransaction();
			s.delete(ygn);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}

	/**
	 * Zapisuje badz aktualizuje obiekt RoomDTO w bazie danych.
	 */
	public long saveCSSemestersYearGroups(CSSemestersYearGroupsDTO csSemestersYearGroupsDTO) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.saveCSSemesterYearGroups(), r: " + csSemestersYearGroupsDTO);
		
		SemestersYearGroupsDTO dto = null;
		
		if (csSemestersYearGroupsDTO.getId() <= 0) {
			dto = new SemestersYearGroupsDTO();
		} else {
			dto = (SemestersYearGroupsDTO)s.load(SemestersYearGroupsDTO.class, new Long(csSemestersYearGroupsDTO.getId()));
		}
		
		//yearGroupName
		YearGroupNameDTO ygn = (YearGroupNameDTO)s.load(YearGroupNameDTO.class, new Long(csSemestersYearGroupsDTO.getYearGroupNameID())); 
		dto.setYearGroupName(ygn);
		
		//semester
		SemesterDTO sem = (SemesterDTO)s.load(SemesterDTO.class, new Long(csSemestersYearGroupsDTO.getSemesterID()));
		dto.setSemester(sem);

		dto.setStudentsNumber(csSemestersYearGroupsDTO.getStudentsNumber());
		
		try {
			s.beginTransaction();
			s.saveOrUpdate(dto);
			s.getTransaction().commit(); s.close();
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
		}
		return dto.getId();
	}
	
	public boolean removeCSSemestersYearGroups(CSSemestersYearGroupsDTO semesterYearGroups) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		//System.out.println("PlannerServiceImpl.removeCSSemesterYearGroups(), syg: " + semesterYearGroups);
		
		
		SemestersYearGroupsDTO syg = (SemestersYearGroupsDTO)s.load(SemestersYearGroupsDTO.class, new Long(semesterYearGroups.getId()));

		try {
			s.beginTransaction();
			s.delete(syg);
			s.getTransaction().commit(); s.close();
			return true;
		} catch ( HibernateException e ) {
			//System.out.println("blad > " + e.toString());
			return false;
		}
	}
	
	
	public CSDeclarationDTO[] getDeclarations() {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		List list = s.createQuery("from DeclarationDTO").list();
		DeclarationDTO[] declarations = new DeclarationDTO[list.size()];
		
		int i=0;
		
		for (Iterator it = list.iterator(); it.hasNext();) {
			DeclarationDTO d = (DeclarationDTO) it.next();
			declarations[i] = d;
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		DeclarationDTO l = declarations[0];
		//s.evict(list);
		
		for (int j = 0; j < declarations.length; j++) {
			//System.out.println(j + ". " + declarations[j]);
		}
		//System.out.println("DECLARATION => " + l.toLabel());
		
		CSDeclarationDTO[] ds = PlannerServiceUtil.convertFromDeclarationDTOToCSDeclarationDTO(declarations);
		return ds;
	}
	
	

	public Boolean signInAdministrator(String login, String password) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
		s.beginTransaction();
		
		String q = 	"from AdminUserDTO au " +
						"where au.login = :login" +
						" and au.password = :password";
		
		Query query = s.createQuery(q).setString("login", login).setString("password",  password);
		
		
		List<AdminUserDTO> list = query.list();
		AdminUserDTO[] adminUsers = list.toArray(new AdminUserDTO[0]);
		
		int i=0;
		
		for (Iterator<AdminUserDTO> it = list.iterator(); it.hasNext();) {
			adminUsers[i] = it.next();
			i++;
		}
		
		s.getTransaction().commit(); s.close();
		
		
		if (adminUsers.length > 0) {
			logger.info("Admin user " + login + " signed in.");
			return new Boolean(true);
		}
		
		logger.info("Admin user " + login + " NOT signed in.");
		return new Boolean(false);
	}
	

	
	
	public TestDTO getTestDTO() {
		TestDTO result = new TestDTO();
		result.setLecturer(new LecturerDTO("janek", "prokul"));
		HashSet atSet = new HashSet();
		atSet.add(new pl.prv.polanie.client.dto.AcademicTitleDTO("dyrek"));
		atSet.add(new pl.prv.polanie.client.dto.AcademicTitleDTO("worek"));
		result.setAcademicTitlesSet(atSet);
		
		return result;
	}
	
	
	
	public static void main(String[] args) {
		
		
		
		PlannerServiceImpl pl = new PlannerServiceImpl();
		
		pl.saveCSLecturer(new CSLecturerDTO(0, "JAAA", "CIE", 1));
		
////		pl.getSubjectEventsByGroupID(new Long(3));
//		pl.getCalendarDaysBySemesterID(new Long(1));
		
//		PlannerServiceImpl pl = new PlannerServiceImpl();
//		PreferenceTypeDTO[] pts = pl.getPreferenceTypes();
//		
//		for (int i = 0; i < pts.length; i++) {
//			//System.out.println("xxx " + i + ". " + pts[i] );
//		}
//		
//		
//		PreferenceDTO[] ps = pl.getPreferences();
//		for (int i = 0; i < ps.length; i++) {
//			//System.out.println("yyy " + i + ". " + ps[i] );
//		}
		
//		CSSubjectEventDTO[] ses = pl.getCSSubjectEventsByGroupID(new Long(1));
//		for (int i = 0; i < ses.length; i++) {
//			//System.out.println("PlannerServiceImpl.main(): " + i + ": " + ses[i]);
//		}
//		
		
//		CSSubjectEventDTO[] ses = pl.getCSSubjectEventsByRoomID(new Long(2), new Long(1));
//		for (int i = 0; i < ses.length; i++) {
//			//System.out.println("PlannerServiceImpl.main(): " + i + ": " + ses[i]);
//		}
		

//		CSSubjectEventDTO[] ses = pl.getCSSubjectEventsByGroupID(new Long(1));
//		for (int i = 0; i < ses.length; i++) {
//			//System.out.println("PlannerServiceImpl.main(): " + i + ": " + ses[i]);
//		}

		
//		CSPreferenceDTO dto = new CSPreferenceDTO(7, 2, 1, 4, new Integer(600), new Integer(90));
//		pl.savePreference(dto);

	}
	
	public static void main2(String[] args) {
		
		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
		
////		try {
//			AcademicTitleDTO at = new AcademicTitleDTO("zmienioan art44");
//			at.setId(3);
//			
//			s.beginTransaction();
//			s.save(at);
//			s.getTransaction().commit(); s.close();
////		} catch ( HibernateException e ) {
////			//System.out.println("blad > " + e.toString());
////		}
		
		
			try {
			s.beginTransaction();
			
			//LecturerDTO l = new LecturerDTO("radek", "kozak");
//			AcademicTitle at = new AcademicTitle("szefuncio");
//			at.setId(7);
			//l.setAcademicTitle(at);
			
//			s.delete(at);
//			YearNumberDTO list = (YearNumberDTO)s.createQuery("from YearNumberDTO where id=:id").setInteger("id",2).uniqueResult();

			s.getTransaction().commit(); s.close();
			
			//System.out.println("l " + list.getNumber() + ", " + list.getId());
		} catch ( ConstraintViolationException e ) {
			logger.warn("ConstraintViolationException. " + e.getMessage());
		} catch ( Exception e ) {
			logger.warn("Exception. " + e.getMessage());
		}

			
		
//		
//		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
//
//		
//		try {
//			s.beginTransaction();
//			
//			//LecturerDTO l = new LecturerDTO("radek", "kozak");
//			AcademicTitle at = new AcademicTitle("szefuncio");
//			at.setId(7);
//			//l.setAcademicTitle(at);
//			
//			s.delete(at);
//			//LecturerDTO list = (LecturerDTO)s.createQuery("from LecturerDTO where id=:id").setInteger("id",3).uniqueResult();
//
//			s.getTransaction().commit(); s.close();
//		} catch ( ConstraintViolationException e ) {
//			logger.warn("ConstraintViolationException. " + e.getMessage());
//		} catch ( Exception e ) {
//			logger.warn("Exception. " + e.getMessage());
//		}

//		
//		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
//		
//		s.beginTransaction();
//		
//		AcademicTitle list = (AcademicTitle)s.createQuery("from AcademicTitle where id=:id").setInteger("id",8).uniqueResult();
//		//AcademicTitle at = new AcademicTitle("������");
//		//s.save(at);
//		//System.out.println("ddd " + list );
//		
////		AcademicTitle at = new AcademicTitle("");
////		s.save(at);
//		//LecturerDTO list = (LecturerDTO)s.createQuery("from LecturerDTO where id=:id").setInteger("id",5).uniqueResult();
//		////System.out.println("XXX " + list);
//		//s.delete(list);
//		//LecturerDTO list = (LecturerDTO)s.createQuery("from LecturerDTO where id=:id").setInteger("id",3).uniqueResult();
//
//		s.getTransaction().commit(); s.close();
		
	}

	
//	private static AcademicTitle getAcademicTitle (int id) {
//		
//		Session s = SessionFactoryHelper.getSessionFactory().openSession();;
//		
//		s.beginTransaction();
//		
//		AcademicTitle list = (AcademicTitle)s.createQuery("from AcademicTitle where id=:id").setInteger("id",id).uniqueResult();
//		s.getTransaction().commit(); s.close();
//		return list;
//	}
	
//	public static 
	
}
